<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        //print_r($_POST);
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        $tab = I("get.tab");
        if($tab==null){
            $tab = 'Home-Index-Main.html';
        }
        $this->assign('name',session('name'));
        $this->assign('uid',session('uid'));
        $this->assign('tab',$tab);
        $this->assign('mobile',session('mobile'));

        $this->display();
    }
    public function Edit(){
        $this->display();
    }
    public function Main(){
        $examination = M("examination");
        $examination_ret = $examination->select();
        //print_r($examination_ret);
        $this->assign('examination_list',$examination_ret);
        $this->display();
    }

    public function submitRun(){

    }

    public function Describe(){
        $examination_id = I("get.examination_id");

        $examination = M("examination");
        $examination_ret = $examination->where("id='$examination_id'")->find();

        $input = explode(' ',$examination_ret['input']);
        $examination_ret['input'] = implode("\n",$input);;
        $this->assign('examination_list',$examination_ret);
        $this->display();
    }


    public function Run(){
        if(session('uid')==null){
            exit("您未登录！");
        }
        //docker方法 docker run -d 969d64bbf1b9  wget oj.projectalice.cn/Application/Data/He/c_cpp/He.c && gcc He.c -o He && ./He
        $ROOT = dirname(dirname(__DIR__));
        ini_set("safe_mode",true);
        ini_set("safe_mode_gid",true);


        $lang = I("get.lang");
        $code = I("post.code");
        $parameter = I("post.parameter");
        $uid  = session('uid');
        $nowtime = time();


        $lasttime = M('lasttime');
        $lasttime_ret = $lasttime->where("uid=$uid")->find();

        if((($lasttime_ret['lasttime']+10)>$nowtime) AND ($lasttime_ret!=null)){
            var_dump($lasttime_ret);
            exit('您请在'.$lasttime_ret['lasttime']+10-$nowtime.'秒后提交，您之前已经提交过一次了！');
        }

        //如果用户没有频繁提交则记录这次提交数据
        $lasttime->data(array('uid'=>$uid,'lasttime'=>$nowtime))->add();


        $user = M('user');
        $user_ret = $user->where("uid=$uid")->find();

        NewDir($ROOT.DIRECTORY_SEPARATOR.'Data'.DIRECTORY_SEPARATOR.$user_ret['name']);//创建用户目录

        if($lang == 'c_cpp'){
            //创建用户C和C++目录
            $dir = $ROOT.DIRECTORY_SEPARATOR.'Data'.DIRECTORY_SEPARATOR.$user_ret['name'].DIRECTORY_SEPARATOR.'c_cpp';
            ini_set("safe_mode_exec_dir",$dir);
            NewDir($dir);
            file_put_contents($dir.DIRECTORY_SEPARATOR.$user_ret['name'].'.c',htmlspecialchars_decode($code));
            $parameter_arr = explode(' ',htmlspecialchars_decode($parameter));
            $parameter_str = implode("\n",$parameter_arr);
            file_put_contents($dir.DIRECTORY_SEPARATOR.$user_ret['name'].'.txt',$parameter_str);


            if(strtolower(substr(PHP_OS, 0, 3)) == 'win'){

                exec("cd ".$dir.DIRECTORY_SEPARATOR." &&gcc.exe -o ".$user_ret['name']." ".$dir.DIRECTORY_SEPARATOR.$user_ret['name'].".c 2>&1 0>&1",$b);
                exec($dir.DIRECTORY_SEPARATOR.$user_ret['name'].".exe<".$dir.DIRECTORY_SEPARATOR.$user_ret['name'].".txt 2>&1",$c);

                $ret_by = '编译结果：';
                $ret_yx = '运行结果：';
                if($b!=null and $b!=''){
                    foreach ($b as $k=>$v){
                        $is_bm = mb_detect_encoding($v);
                        $ret_by .= iconv($is_bm,'utf-8',$v)."\n";
                    }
                }


                foreach ($c as $k=>$v){
                    $is_bm = mb_detect_encoding($v);
                    $ret_yx .= iconv($is_bm,'utf-8',$v)."\n";
                }

                //将运行数据添加到数据库
                $x_code_array = array(
                    'lang'=>$lang,
                    'code'=>$code,
                    'parameter'=>$parameter,
                    'result'=>htmlspecialchars($ret_by).'<br>'.htmlspecialchars($ret_yx),
                    'date'=>$nowtime,
                    'uid'=>$uid
                );
                $x_code = M("code");
                $x_code->data($x_code_array)->add();


                exit(htmlspecialchars($ret_by).'<br>'.htmlspecialchars($ret_yx));

                /*var_dump($b);
                var_dump($c);*/
            }else{

                //Linux适配未完成
                exec("cd ".$ROOT.DIRECTORY_SEPARATOR."Data && gcc -o A ".$ROOT.DIRECTORY_SEPARATOR."Data".DIRECTORY_SEPARATOR."A.c 2>&1 0>&1",$b);
                exec("cd ".$ROOT.DIRECTORY_SEPARATOR."Data && ./A 2>&1 0>&1",$c);

                var_dump($b);
                var_dump($c);
            }
        }elseif($lang == 'java'){
            //创建用户java目录
            $dir = $ROOT.DIRECTORY_SEPARATOR.'Data'.DIRECTORY_SEPARATOR.$user_ret['name'].DIRECTORY_SEPARATOR.'java';
            NewDir($dir);
            ini_set("safe_mode_exec_dir",$dir);
            file_put_contents($dir.DIRECTORY_SEPARATOR.$user_ret['name'].'.java',htmlspecialchars_decode($code));
            $parameter_arr = explode(' ',htmlspecialchars_decode($parameter));
            $parameter_str = implode("\n",$parameter_arr);
            file_put_contents($dir.DIRECTORY_SEPARATOR.$user_ret['name'].'.txt',implode("\n",$parameter_str));

            if(strtolower(substr(PHP_OS, 0, 3)) == 'win'){
                exec("javac.exe ".$dir.DIRECTORY_SEPARATOR.$user_ret['name'].".java 2>&1",$b);
                //echo "'C:\Program Files\Java\jdk1.8.0_31\bin\javac.exe' ".$dir.DIRECTORY_SEPARATOR.$user_ret['name'].".java 2>&1";
                exec("d: && cd ".$dir." && java ".$user_ret['name']."<".$user_ret['name'].".txt 2>&1",$c);

                $ret_by = '编译结果：';
                $ret_yx = '运行结果：';
                if($b!=null and $b!=''){
                    foreach ($b as $k=>$v){
                        $is_bm = mb_detect_encoding($v);
                        $ret_by .= iconv($is_bm,'utf-8',$v)."\n";
                    }
                }

                foreach ($c as $k=>$v){
                    $is_bm = mb_detect_encoding($v);
                    $ret_yx .= iconv($is_bm,'utf-8',$v)."\n";
                }

                //将运行数据添加到数据库
                $x_code_array = array(
                    'lang'=>$lang,
                    'code'=>$code,
                    'parameter'=>$parameter,
                    'result'=>htmlspecialchars($ret_by).'<br>'.htmlspecialchars($ret_yx),
                    'date'=>$nowtime,
                    'uid'=>$uid
                );
                $x_code = M("code");
                $x_code->data($x_code_array)->add();


                exit(htmlspecialchars($ret_by).'<br>'.htmlspecialchars($ret_yx));
                /*var_dump($b);
                var_dump($c);*/
            }else{
                //Linux适配未完成
                exec("javac ".$ROOT.DIRECTORY_SEPARATOR."Data".DIRECTORY_SEPARATOR."A.java 2>&1",$b);
                exec("cd ".$ROOT.DIRECTORY_SEPARATOR."Data && java A 2>&1",$c);

                var_dump($b);
                var_dump($c);
            }

        }else{
            exit('未适配成功！');
        }
    }
}